<?php

namespace app\api\controller\admin\setting;

use app\api\common\controller\BaseApi;
use app\Request;
use app\api\services\admin\setting\PermissionServices;
use think\App;

class PermissionController extends BaseApi
{
    /**
     * @var PermissionServices
     */
    private PermissionServices $services;

    public function __construct(Request $request, App $app, PermissionServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 显示资源列表
     *
     * @param \app\Request $request
     *
     * @return \think\Response
     */
    public function select(Request $request): \think\Response
    {

        $where = $request->getParams([
            ['name'],
            ['delete_time', null],
            ['status'],
        ]);

        $this->validate($where, [
            'name'   => 'max:32',
            'status' => 'in:0,1',
        ], [], true, true);

        $ret = $this->services->selectList($where, "id, pid, name, icon, type, status, show, api_path, methods, menu_path, sort, unique_auth", 0, 0, 'sort ASC', [])->toArray();

        return ret_value(0, "", $ret);
    }

    /**
     *  显示指定资源
     *
     * @param $id
     *
     * @return \think\response\Json
     */
    public function get($id): \think\response\Json
    {
        $where = ['id' => $id];
        $ret   = $this->services->get($where)->toArray();
        return ret_value(0, "", $ret);
    }

    /**
     * 新建
     *
     * @param \app\Request $request
     *
     * @return \think\Response
     */
    public function insert(Request $request): \think\Response
    {
        $data = $request->getParams([
            ['pid', 0],
            ['name'],
            ['type', 0],
            ['status', 0],
            ['sort', 0],
            ['api_path'],
            ['methods', ''],
            ['show', 0],
            ['icon', ''],
            ['menu_path', ''],
        ]);

        $this->validate($data, [
            'pid'       => 'require|integer',
            'name'      => 'require|max:32',
            'type'      => 'require|integer',
            'status'    => 'in:0,1',
            'sort'      => 'require|integer',
            'api_path'  => 'max:255',
            'methods'   => 'in:POST,GET,PUT,DELETE',
            'show'      => 'require|integer',
            'icon'      => 'max:32',
            'menu_path' => 'max:255',
        ], [], true, true);

        $this->services->save($data);
        return ret_value(0, "Success");
    }

    /**
     * 修改
     *
     * @param \app\Request $request
     *
     * @return \think\response\Json
     */
    public function update(Request $request): \think\response\Json
    {
        $where = $request->getParams([['id']]);
        $data  = $request->getParams([
            ['pid'],
            ['name'],
            ['icon'],
            ['type'],
            ['status'],
            ['show'],
            ['api_path'],
            ['methods'],
            ['menu_path'],
            ['sort'],
            ['unique_auth'],
            ['delete_time'],
        ]);
        $this->validate($where, [
            'id' => 'require',
        ], [], true, true);
        $this->validate($data, [
            'pid'       => 'integer',
            'name'      => 'max:32',
            'type'      => 'integer',
            'status'    => 'in:0,1',
            'sort'      => 'integer',
            'api_path'  => 'max:255',
            'methods'   => 'in:POST,GET,PUT,DELETE',
            'show'      => 'integer',
            'icon'      => 'max:32',
            'menu_path' => 'max:255',
        ], [], true, true);

        $ret = $this->services->update($where, $data);
        if ($ret)
            return ret_value(0, "Success");
        else
            return ret_value(-1, "Fail");

    }

    /**
     * 删除管理员
     *
     * @param \app\Request $request
     *
     * @return \think\response\Json
     */
    public function delete(Request $request): \think\response\Json
    {
        $data = $request->getParams([
            ['data'],
        ]);
        $this->validate($data, [
            'data' => 'require',
        ]);
        $ids = explode(',', $data["data"]);
        $ret = $this->services->destroy($ids);
        if ($ret)
            return ret_value(0, "Success");
        else
            return ret_value(-1, "Fail");
    }

    /**
     * 获取权限菜单
     *
     * @param \app\Request $request
     *
     * @return \think\response\Json
     * @throws \ReflectionException
     */
    public function permission(Request $request): \think\response\Json
    {
        $user  = $request->AdminInfo ?? [];
        $where = ['status' => 1, 'delete_time' => null, 'type' => 1];
        if ($user['level'] > 0) {
            $where['id_in'] = $user['roles'];
        }

        $ret = $this->services->rule($where);

        return ret_value(0, 'ok', $ret['item']);
    }
}
